package ex.com.jm.food.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import ex.com.jm.food.dao.AdminAttrDao;
import ex.com.jm.food.entity.Attr;
import ex.com.jm.food.entity.SubType;

/**
 * 
 * @author zhaojw
 *
 */
public class AdminAttrDaoImpl extends HibernateDaoSupport implements
		AdminAttrDao {

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Attr> getAttrs(final int initPage,final int pageRow,final String name,
			final Integer type,final Integer subId, final String sort,final String order) {
		// TODO Auto-generated method stub
		Object attr=this.getHibernateTemplate().execute(new HibernateCallback(){

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql="select new ex.com.jm.food.entity.dto.AttrDTO(attr.attrId,attr.attrName,attr.attrValue,attr.attrType,sub.subName,attr.created) "
						+ "from Attr attr left join attr.subId sub  where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql+=" and attr.attrName like '%"+name+"%'";
				}
				if(type!=null&&type.equals("")){
					hql+=" and attr.attrType="+type;
				}
				if(subId!=null&&!subId.equals("")){
					hql+=" and attr.subId="+subId;
				}
				if(sort!=null&&!sort.equals("")&&order!=null&&!order.equals("")){
					hql+=" order by "+order+" "+sort;
				}
				Query query=session.createQuery(hql).setFirstResult(initPage).setMaxResults(pageRow);
				return query.list();
			}
		});
		return (List<Attr>) attr;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public int attrCounts(final String name,final Integer type,final Integer subId) {
		// TODO Auto-generated method stub
		int count=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				String hql=" from Attr attr where 1=1 ";
				if(name!=null&&!name.equals("")){
					hql+=" and attr.attrName like '%"+name+"%'";
				}
				if(type!=null&&type.equals("")){
					hql+=" and attr.attrType="+type;
				}
				if(subId!=null&&!subId.equals("")){
					hql+=" and attr.subId="+subId;
				}

				Query query=session.createQuery(hql);
				return query.list().size();
			}
		});
		return count;
	}

	@Override
	public Object saveAttr(Attr attr) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().save(attr);
	}

	@Override
	public void updateAttr(Attr attr) {
		// TODO Auto-generated method stub
           this.getHibernateTemplate().update(attr);
	}

	@Override
	public void delAttr(int id) {
		// TODO Auto-generated method stub
        Attr attr=this.getHibernateTemplate().get(Attr.class, id);
        this.getHibernateTemplate().delete(attr);
	}

	@Override
	public Attr editAttr(int id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Attr.class, id);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Attr checkName(final String attrName) {
		// TODO Auto-generated method stub
		Attr attr=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from Attr where attrName='"+attrName+"'");
				return query.uniqueResult();
			}
		});
		return attr;
	}

	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public List<SubType> getSubTypes() {
		// TODO Auto-generated method stub
		Object subTypes=this.getHibernateTemplate().execute(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Query query=session.createQuery("from SubType");
				return query.list();
			}
		});
		return (List<SubType>) subTypes;
	}

	@Override
	public SubType getSubType(Integer subId) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(SubType.class, subId);
	}

}
